<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.name" placeholder="请输入服务名称..."></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="products" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>

      <el-table-column prop="name" label="服务名称" width="120" sortable>
      </el-table-column>

      <el-table-column prop="resources" label="服务资源" width="120" sortable>
      </el-table-column>

      <el-table-column prop="saleprice" label="售价" width="120" sortable>
      </el-table-column>

      <el-table-column prop="costprice" label="成本价" width="120" sortable>
      </el-table-column>

      <el-table-column prop="onsaletime" label="上架时间" width="120" sortable>
      </el-table-column>

      <el-table-column prop="offsaletime" label="下架时间" width="120" sortable>
      </el-table-column>

      <el-table-column prop="createtime" label="创建时间" width="120" sortable>
      </el-table-column>

      <el-table-column prop="salecount" label="销量" width="120" sortable>
      </el-table-column>

      <el-table-column prop="state" label="状态" width="120" sortable>
        <template scope="scope">
          <span style="color:#6cf805;" v-if="scope.row.state === 1">启用</span>
          <span style="color:#bd0707" v-else-if="scope.row.state === 0">禁用</span>
          <span style="color:#292424" v-else>未知</span>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize"
                     :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!-- 新增和修改的界面 -->
    <el-dialog :title="title" :visible.sync="productVisible" :close-on-click-modal="false">
      <el-form :model="productForm" label-width="80px" :rules="productFormRules" ref="productForm">
        <el-form-item label="服务名称" prop="name">
          <el-input v-model="productForm.name" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="服务资源" prop="resources">
          <el-input v-model="productForm.resources" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="售价" prop="saleprice">
          <el-input v-model="productForm.saleprice" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="上架时间" prop="onsaletime">
          <el-input v-model="productForm.onsaletime" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="下架时间" prop="offsaletime">
          <el-input v-model="productForm.offsaletime" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="成本价" prop="costprice">
          <el-input v-model="productForm.costprice" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="创建时间" prop="createtime">
          <el-input v-model="productForm.createtime" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="销量" prop="salecount">
          <el-input v-model="productForm.salecount" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="状态">
          <el-radio-group v-model="productForm.state">
            <el-radio class="radio" :label="1">启用</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="productVisible = false">取消</el-button>
        <el-button type="primary" @click.native="productSubmit" :loading="productLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'
import {getUserListPage, removeUser, batchRemoveUser, editUser, addUser} from '../../api/api';

export default {
  data() {
    return {
      filters: {
        username: ''
      },
      products: [],
      currentPage: 1,
      total: 0,
      pageSize: 10,
      title:'',
      listLoading: false,
      sels: [],//列表选中列
      productVisible: false,//编辑界面是否显示
      productLoading: false,
      productFormRules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ],
        resources: [
          {required: true, message: '请上传资源', trigger: 'blur'}
        ],
        saleprice: [
          {required: true, message: '请输入售价', trigger: 'blur'}
        ],
        onsaletime: [
          {required: true, message: '请输入上架时间', trigger: 'blur'}
        ],
        offsaletime: [
          {required: true, message: '请输入下架时间', trigger: 'blur'}
        ],
        state: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ]
      },
      //编辑界面数据
      productForm: {
        id: null,
        name: '',
        resources: '',
        saleprice: null,
        onsaletime: '',
        offsaletime: '',
        state: ''
      },
    }
  },
  methods: {
    search(){
      this.currentPage = 1;
      /*this.getDepartments();*/
    },

    /*	//性别显示转换
    formatSex: function (row, column) {
      return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
    },*/
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.getProducts();
    },
    //获取用户列表
    getProducts() {
      let para = {
        currentPage: this.currentPage,
        name: this.filters.name,
        pageSize: this.pageSize
      };
      this.listLoading = true;// 开启忙等框
      //NProgress.start();
      this.$http.post("/product", para)// 请求地址：后台的dept接口
          .then(result => {
            result = result.data;// 将列表数据PageList{total rows}赋值给result
            this.products = result.rows;

            this.listLoading = false;// 关闭忙等框
            this.total = result.total;
          })
          .catch(result => {
            console.log(result);
            alert("系统快爆炸了！");
          })


      /*getUserListPage(para).then((res) => {
        this.total = res.data.total;
        this.products = res.data.products;
        this.listLoading = false;
        //NProgress.done();
      });*/
    },

    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '强制提醒', {// 弹出删除提示框
        type: 'warning'
      }).then(() => {// 如果成功，则执行以下操作
        this.listLoading = true;// 打开忙等框
        let para = {id: row.id};// 获取当前行的id
        this.$http.delete("/product/" + row.id)// 发起axios请求
            .then(result => {// 如果成功，则提示用户
              this.listLoading = false;// 关闭忙等框
              if (result.data.success) {
                this.$message({
                  message: "删除成功",
                  type: "success"
                })
              } else {
                this.$message({
                  message: "删除失败",
                  type: "error"
                })
              }
              this.currentPage = 1;// 删除成功后再定位到第一页
              this.getProducts();// 重新刷新列表

            }).catch(result => {// 不成功则发送错误信息提醒用户
          this.listLoading = false;// 关闭忙等框
          this.$message({
            message: "删除失败",
            type: "error"
          })
        })

      }).catch(() => {// 如果用户点击取消，则不执行任何操作

      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.title = "修改";
      this.productVisible = true;
      this.productForm = Object.assign({}, row);
    },
    //显示新增界面
    handleAdd: function () {
      this.productVisible = true;
      this.title = "添加",
      this.productForm = {
        id: null,
        name: '',
        resources: '',
        saleprice: null,
        onsaletime: '',
        offsaletime: '',
        state: ''
      };
    },
    // 提交按钮绑定
    productSubmit: function () {
      // validate开启表单校验
      this.$refs.productForm.validate((valid) => {
        if (valid) {// 如果校验通过，执行下面操作
          this.$confirm('确认提交吗？', '强制提示', {}).then(() => {
            this.productLoading = true;// 开启忙等框
            let para = Object.assign({}, this.productForm);// 复制表单数据
            if (para.parent) {
              let parent = para.parent;
              let pid = parent[parent.length - 1];
              para.parent = {"id": pid};
            }
            this.$http.put("/product", para)
                .then(result => {
                  result = result.data;
                  this.productLoading = false;// 关闭忙等框
                  this.productVisible = false;// 关闭模态框
                  if (result.success) {
                    this.$message({
                      message: "操作成功！",
                      type: "success"
                    });
                  } else {
                    this.$message({
                      message: "操作失败！！",
                      type: "error"
                    });
                  }

                  this.page = 1;// 跳转到第一页
                  this.getProducts();// 重新刷新页面
                })
                .catch(result => {
                  this.productLoading = false;
                  this.$message({
                    message: "操作失败",
                    type: "error"
                  })
                })
          });
        }
      });
    },

    selsChange: function (sels) {
      this.sels = sels;
    },

    // 批量删除，
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '强制提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;// 打开忙等框
        this.$http.patch("/product", ids)
            .then(result => {// 如果成功，就走这里
              result = result.data;
              if (result.success) {
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
              } else {
                this.$message({
                  message: '删除失败',
                  type: 'error'
                });
              }
              this.currentPage = 1;// 删除成功后再定位到第一页
              this.getProducts();// 重新刷新列表
            })
            .catch(result => {
              alert("系统删除错误")
            })
            .catch(result => {// 不成功，就没动作
            })
      })
          .catch(() => {
          });
    },
  },

  mounted() {
    this.getProducts();
  }
}
</script>

<style scoped>

</style>